﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.AccessControl;
using System.IO;

namespace FileLib
{
    /// <summary>
    /// 文件访问权限
    /// </summary>
    public class FileAccessControl
    {
        /// <summary>
        /// 为指定帐户的指定文件增加权限
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="account">用户账户名称</param>
        /// <param name="rights">操作访问规则</param>
        /// <param name="controlType">允许 | 拒绝该操作</param>
        public static void AddFileSecurity(string fileName, string account,
            FileSystemRights rights, AccessControlType controlType)
        {
            FileSecurity security = File.GetAccessControl(fileName);
            security.AddAccessRule(new FileSystemAccessRule(account,
                rights, controlType));
            File.SetAccessControl(fileName, security);
        }

        /// <summary>
        /// 为指定帐户的指定文件移除权限
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="account">用户账户名称</param>
        /// <param name="rights">操作访问规则</param>
        /// <param name="controlType">允许 | 拒绝该操作</param>
        public static void RemoveFileSecurity(string fileName, string account,
            FileSystemRights rights, AccessControlType controlType)
        {
            FileSecurity security = File.GetAccessControl(fileName);
            security.RemoveAccessRule(new FileSystemAccessRule(account,
                rights, controlType));
            File.SetAccessControl(fileName, security);
        }
    }
}
